import { http } from '@renderer/plugins/axios'
import { ElMessage } from 'element-plus'
import { ref } from 'vue'
import useUtil from './useUtil'

export default () => {
  const peopleList = ref<ApiPage<PeopleListModel>>()
  const disbursePersonList = ref<ApiPage<DisbursePersonListModel>>()
  const getPersonList = useUtil().request(async (data) => {
    try {
      peopleList.value = await http.request({
        url: '/getPersonList',
        method: 'POST',
        data,
      })
    } catch (error) {
      console.log(error)
    }
  })
  const getDisbursePersonList = useUtil().request(async (data) => {
    try {
      disbursePersonList.value = await http.request({
        url: '/getDisbursePersonList',
        method: 'POST',
        data,
      })
    } catch (error) {
      console.log(error)
    }
  })
  const delPerson = useUtil().request(async (data) => {
    try {
      await http.request({
        url: '/delPerson',
        method: 'POST',
        data,
      })
      ElMessage({ type: 'success', message: '操作成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const delDisbursePerson = useUtil().request(async (data) => {
    try {
      await http.request({
        url: '/delDisbursePerson',
        method: 'POST',
        data,
      })
      ElMessage({ type: 'success', message: '操作成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const getDisbursePersonCate = useUtil().request(async (params) => {
    try {
      const cateList = await http.request({
        url: '/getDisbursePersonCate',
        method: 'GET',
        params,
      })
      return cateList
    } catch (error) {
      console.log(error)
      return null
    }
  })
  const addPerson = useUtil().request(async (data) => {
    try {
      const res = await http.request({
        url: '/addPerson',
        method: 'POST',
        data,
      })
      if (res) {
        ElMessage({ type: 'success', message: '操作成功' })
        return true
      } else {
        return false
      }
    } catch (error) {
      console.log(error)
      return null
    }
  })
  const setPersonRule = useUtil().request(async (data) => {
    try {
      const res = await http.request({
        url: '/setPersonRule',
        method: 'POST',
        data,
      })
      if (res) {
        ElMessage({ type: 'success', message: '操作成功' })
        return true
      } else {
        return false
      }
    } catch (error) {
      console.log(error)
      return null
    }
  })
  const updatePersonInfo = useUtil().request(async (data) => {
    try {
      const res = await http.request({
        url: '/updatePersonInfo',
        method: 'POST',
        data,
      })
      if (res) {
        ElMessage({ type: 'success', message: '操作成功' })
        return true
      } else {
        return false
      }
    } catch (error) {
      console.log(error)
      return null
    }
  })
  const addDisbursePerson = useUtil().request(async (data) => {
    try {
      const res = await http.request({
        url: '/addDisbursePerson',
        method: 'POST',
        data,
      })
      if (res) {
        ElMessage({ type: 'success', message: '操作成功' })
        return true
      } else {
        return false
      }
    } catch (error) {
      console.log(error)
      return null
    }
  })
  const updateDisbursePerson = useUtil().request(async (data) => {
    try {
      const res = await http.request({
        url: '/updateDisbursePerson',
        method: 'POST',
        data,
      })
      if (res) {
        ElMessage({ type: 'success', message: '操作成功' })
        return true
      } else {
        return false
      }
    } catch (error) {
      console.log(error)
      return null
    }
  })
  const getPersonDetail = useUtil().request(async (params) => {
    try {
      console.log(params, 'params')
      const userInfo = await http.request({
        url: '/getPersonDetail',
        method: 'GET',
        params,
      })
      console.log(userInfo, 'userInfo')
      return userInfo
    } catch (error) {
      console.log(error)
      return null
    }
  })
  const getDisbursePersonDetail = useUtil().request(async (params) => {
    try {
      const userInfo = await http.request({
        url: '/getDisbursePersonDetail',
        method: 'GET',
        params,
      })
      return userInfo
    } catch (error) {
      console.log(error)
      return null
    }
  })
  return {
    getPersonList,
    peopleList,
    delPerson,
    getDisbursePersonList,
    delDisbursePerson,
    getDisbursePersonCate,
    disbursePersonList,
    addPerson,
    getPersonDetail,
    updatePersonInfo,
    updateDisbursePerson,
    addDisbursePerson,
    getDisbursePersonDetail,
    setPersonRule,
  }
}
